<?php

function __autoload($ten){

	$ten=str_replace("_","/",$ten);

	$ten=strtolower(str_replace("Model","Models",$ten));

	require("$ten.php");

}



function loadview($ten,$data=""){

	require("views/$ten.php");

}



function redirect($url){

	ob_end_clean();

	header("location:$url");

	exit();

}

function redirectjs($str,$url){

	loadview("template/header2");

	echo "<script>alert('".$str."');location.href='".$url."'</script>";

	exit();

	

	

	

}

function filter($string)

{

$string1=$string;

$thay='*';

return preg_replace("/(location\.)|(alert)|(document\.)|(script)|(implementation)|(livescript)|(LAYER)|(mocha)|(select)|(union)|(concat)/i", $thay, $string1);



}

if (get_magic_quotes_gpc()) {

 function stripslashes_deep($value)

 	{

       $value = is_array($value) ?

        array_map('stripslashes_deep', $value) :

                    stripslashes(filter($value));



        return $value;

	}  

	  $_POST =stripslashes_deep($_POST);

	  $_GET =stripslashes_deep($_GET);

}







 function khongdau($str) { 

	$str = preg_replace("(%20| )", '-', $str);

    $str = preg_replace("/(\.|\/|_|!|>|<|^|,|;|#|%|'|:|\"|\?|=)/", '', $str);  

    $str = str_replace('----','-',$str);  

    $str = str_replace('---','-',$str);  

    $str = str_replace('--','-',$str);  

	$str = preg_replace("(\()", '', $str);

	$str = preg_replace("(\))", '', $str); 	

   $str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);

	$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);

	$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);

	$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);

	$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);

	$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);

	$str = preg_replace("/(đ)/", 'd', $str);

	$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);

	$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);

	$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);

	$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);

	$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);

	$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);

	$str = preg_replace("/(Đ)/", 'D', $str);

	$str = preg_replace("/'/", '', $str); 

	$str = preg_replace("//", '', $str); 

	$str = preg_replace("/%/", '', $str); 

	return $str; 

}











function url() {
 $pageURL = 'http';

 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}

 $pageURL .= "://";

 if ($_SERVER["SERVER_PORT"] != "80") {

  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];

 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}



function echo2($str)
{

	 echo htmlspecialchars($str,ENT_QUOTES,'utf-8');

}

function get_ip()
{
     $ip = false;
     if(!empty($_SERVER['HTTP_CLIENT_IP']))
     {
          $ip = $_SERVER['HTTP_CLIENT_IP'];
     }
     if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
     {
          $ips = explode(", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
          if($ip)
          {
              array_unshift($ips, $ip);
               $ip = false;
          }
          for($i = 0; $i < count($ips); $i++)
          {
               if(!preg_match("/^(10|172\.16|192\.168)\./i", $ips[$i]))
               {
                    if(version_compare(phpversion(), "5.0.0", ">="))
                    {
                         if(ip2long($ips[$i]) != false)
                         {
                              $ip = $ips[$i];
                              break;
                        }

                    }

                    else

                    {

                         if(ip2long($ips[$i]) != - 1)
                         {
                              $ip = $ips[$i];
                             break;

                         }

                    }

               }

          }

     }

     return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);

}  



function is_mobile() {
	// Get the user agent



	$user_agent = $_SERVER['HTTP_USER_AGENT'];



	// Create an array of known mobile user agents

	// This list is from the 21 October 2010 WURFL File.

	// Most mobile devices send a pretty standard string that can be covered by

	// one of these.  I believe I have found all the agents (as of the date above)

	// that do not and have included them below.  If you use this function, you 

	// should periodically check your list against the WURFL file, available at:

	// http://wurfl.sourceforge.net/





	$mobile_agents = Array(





		"240x320",

		"acer",

		"acoon",

		"acs-",

		"abacho",

		"ahong",

		"airness",

		"alcatel",

		"amoi",	

		"android",

		"anywhereyougo.com",

		"applewebkit/525",

		"applewebkit/532",

		"asus",

		"audio",

		"au-mic",

		"avantogo",

		"becker",

		"benq",

		"bilbo",

		"bird",

		"blackberry",

		"blazer",

		"bleu",

		"cdm-",

		"compal",

		"coolpad",

		"danger",

		"dbtel",

		"dopod",

		"elaine",

		"eric",

		"etouch",

		"fly " ,

		"fly_",

		"fly-",

		"go.web",

		"goodaccess",

		"gradiente",

		"grundig",

		"haier",

		"hedy",

		"hitachi",

		"htc",

		"huawei",

		"hutchison",

		"inno",

		"ipad",

		"ipaq",

		"ipod",

		"jbrowser",

		"kddi",

		"kgt",

		"kwc",

		"lenovo",

		"lg ",

		"lg2",

		"lg3",

		"lg4",

		"lg5",

		"lg7",

		"lg8",

		"lg9",

		"lg-",

		"lge-",

		"lge9",

		"longcos",

		"maemo",

		"mercator",

		"meridian",

		"micromax",

		"midp",

		"mini",

		"mitsu",

		"mmm",

		"mmp",

		"mobi",

		"mot-",

		"moto",

		"nec-",

		"netfront",

		"newgen",

		"nexian",

		"nf-browser",

		"nintendo",

		"nitro",

		"nokia",

		"nook",

		"novarra",

		"obigo",

		"palm",

		"panasonic",

		"pantech",

		"philips",

		"phone",

		"pg-",

		"playstation",

		"pocket",

		"pt-",

		"qc-",

		"qtek",

		"rover",

		"sagem",

		"sama",

		"samu",

		"sanyo",

		"samsung",

		"sch-",

		"scooter",

		"sec-",

		"sendo",

		"sgh-",

		"sharp",

		"siemens",

		"sie-",

		"softbank",

		"sony",

		"spice",

		"sprint",

		"spv",

		"symbian",

		"tablet",

		"talkabout",

		"tcl-",

		"teleca",

		"telit",

		"tianyu",

		"tim-",

		"toshiba",

		"tsm",

		"up.browser",

		"utec",

		"utstar",

		"verykool",

		"virgin",

		"vk-",

		"voda",

		"voxtel",

		"vx",

		"wap",

		"wellco",

		"wig browser",

		"wii",

		"windows ce",

		"wireless",

		"xda",

		"xde",

		"zte"

	);



	// Pre-set $is_mobile to false.



	$is_mobile = false;



	// Cycle through the list in $mobile_agents to see if any of them

	// appear in $user_agent.



	foreach ($mobile_agents as $device) {



		// Check each element in $mobile_agents to see if it appears in

		// $user_agent.  If it does, set $is_mobile to true.



		if (stristr($user_agent, $device)) {



			$is_mobile = true;



			// break out of the foreach, we don't need to test

			// any more once we get a true value.



			break;

		}

	}

return $is_mobile;

}
function randomstring($length = 10) {
    return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
}
